/*
 * Copyright (C) 2007,2008  Jonathan Hendler
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 * 
 * The authors can be contacted at: 
 * @author Jonathan Hendler [jonathan DOT hendler AT google DOT com]
 * 
 * @license GPLv2
 */

/**
 * 
 * NYMRL allows arbitrary network relationships - P2P, services, Masters, Clients, etc., all
 * over HTTP
 * Logic for the network relationships is determined by the implmentation for data replication and synchronization.
 * 
 * Concepts:
 * 	1. node - an ID for a collection of information like a userID
 * 		a user id can exist at multiple URLS, so a node is not a machine ID
 * 		create of an ID must happen once, in one place, and be propogated through the
 * 		entire system. Any node can create node IDs, but only ONE node should be allowed to
 * 		do so. Propogation of the ID must happen in a way that syncronization locks are not a problem.
 * 		
 *  2. relationship, a name for an arbitrary connection type between nodes
 *      for example, these can be "master", child, parent, or any other relation like login, etc
 * 		a relationship serves as a "group" in a sense 
 *  3. url
 * 		is a machine id, address for processing
 * 
 *  NODE (1) --- (*) Relationship  --- (*) URL  
 * 
 * A URL provides the physical address of a machine. And a node can live on multiple machines
 * 
 * @todo config file for 
 * @todo logging
 * @todo add BDB in addition to SQLite
 * @
 * */
#ifndef NYMRL_H_
#define NYMRL_H_ 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

//helpers
#include "nymrl_message.h"
#include "nymrl_conf.h"
#include "nymrl_log.h"
#include "nymrl_helper.h"
#include "nymrl_sqlite.h"

/**
 * call back must be set by user 
 * nymrl_dynamic_function = &[my_function]
 * which returns and accepts two inputs, a char * for an action, then the data
 * 
 * this can be registered one time at the launch of app
 * 
 * @see _nymrl_do_callback()
 * */
char * (*nymrl_callback_function)(char*,char *);


/**
 * //TODO
 * */
inline int _nyml_get_ids_valid(NYMRL_ID id, char * rel);

/**
 * Sends a request, via CURL, to the urls found from the query for nid, rel, and any or all
 * 
 * if NID or REL is null
 * 
 * gets the response, processes the header automatically to check
 * and returns the reply, without headers only. 
 * The reply will also be a string following the form of URLencoded data.
 * With content length. 
 * 
 * */
inline nyml_header_struct * nyml_request(nyml_header_struct *  header);

/**
 * forwards requests over HTTP 
 * **/
inline nyml_header_struct *  _nymrl_do_forward(NYMRL_URL_LIST urls, int url_count, nyml_header_struct *  header);

/**
 * 
 * */
inline char * _nymrl_do_callback(char* act, char * dat);

/**
 * after receiving and processing, the header struct can be modified
 * and passed here to generate a string
 * each param is "key","value" and the = or & will be appended
 * */
inline char * nymrl_to_string( nyml_header_struct * header, ...);

 
////////////////////////
/**
 * HELPERS
 * */

/**
 * check that an ID is local
 * */
inline int nyml_get_ids_valid(NYMRL_ID id, char * rel);

inline NYMRL_URL_LIST _nymrl_get_url_switch(NYMRL_ID nid,char * rel,int first_val,int * url_count);


inline char * nymrl_cgi_helper(char * post_content);

#endif /*NYMRL_H_*/
